我正在尝试从Azure AD系统中检索身份验证令牌。我已经尝试了许多不同的方法来配置async方法和await命令,但每次我收到错误“任务被取消”。
我的aspx页面中有async =“true”。
任何想法,我需要采取不同的做法,以获得成功的请求和检索令牌。
相同的代码在控制台应用程序中工作,因此,我假设问题与异步操作的发生方式有关。
我的代码如下:
[Loaded sun.util.logging.PlatformLogger$1 from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.util.logging.PlatformLogger$LoggerProxy from C:\Program Files\Java\j
re7\lib\rt.jar]
[Loaded sun.util.logging.PlatformLogger$DefaultLoggerProxy from C:\Program Files
\Java\jre7\lib\rt.jar]
[Loaded sun.util.logging.PlatformLogger$JavaLoggerProxy from C:\Program Files\Ja
va\jre7\lib\rt.jar]
[Loaded sun.util.logging.LoggingSupport from C:\Program Files\Java\jre7\lib\rt.j
ar]
[Loaded sun.util.logging.LoggingSupport$1 from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.util.logging.LoggingProxy from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded java.util.logging.LoggingProxyImpl from C:\Program Files\Java\jre7\lib\r
t.jar]
[Loaded sun.reflect.UnsafeFieldAccessorFactory from C:\Program Files\Java\jre7\l
ib\rt.jar]
[Loaded sun.reflect.UnsafeQualifiedStaticFieldAccessorImpl from C:\Program Files
\Java\jre7\lib\rt.jar]
[Loaded sun.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl from C:\Program
Files\Java\jre7\lib\rt.jar]
[Loaded sun.util.logging.LoggingSupport$2 from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded java.util.Date from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.util.calendar.CalendarSystem from C:\Program Files\Java\jre7\lib\rt.
jar]
[Loaded sun.util.calendar.AbstractCalendar from C:\Program Files\Java\jre7\lib\r
t.jar]
[Loaded sun.util.calendar.BaseCalendar from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded sun.util.calendar.Gregorian from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.Transparency from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.DisposerTarget from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.StateTrackable from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.Surface from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.SurfaceData from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.lang.IllegalStateException from C:\Program Files\Java\jre7\lib\rt.j
ar]
[Loaded sun.java2d.InvalidPipeException from C:\Program Files\Java\jre7\lib\rt.j
ar]
[Loaded sun.java2d.NullSurfaceData from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.StateTrackable$State from C:\Program Files\Java\jre7\lib\rt.j
ar]
[Loaded sun.java2d.loops.SurfaceType from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.image.PixelConverter from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.lang.Integer$IntegerCache from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded sun.awt.image.PixelConverter$Xrgb from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.awt.image.PixelConverter$Argb from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.awt.image.PixelConverter$ArgbPre from C:\Program Files\Java\jre7\lib
\rt.jar]
[Loaded sun.awt.image.PixelConverter$Xbgr from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.awt.image.PixelConverter$Rgba from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.awt.image.PixelConverter$RgbaPre from C:\Program Files\Java\jre7\lib
\rt.jar]
[Loaded sun.awt.image.PixelConverter$Ushort565Rgb from C:\Program Files\Java\jre
7\lib\rt.jar]
[Loaded sun.awt.image.PixelConverter$Ushort555Rgb from C:\Program Files\Java\jre
7\lib\rt.jar]
[Loaded sun.awt.image.PixelConverter$Ushort555Rgbx from C:\Program Files\Java\jr
e7\lib\rt.jar]
[Loaded sun.awt.image.PixelConverter$Ushort4444Argb from C:\Program Files\Java\j
re7\lib\rt.jar]
[Loaded sun.awt.image.PixelConverter$ByteGray from C:\Program Files\Java\jre7\li
b\rt.jar]
[Loaded sun.awt.image.PixelConverter$UshortGray from C:\Program Files\Java\jre7\
lib\rt.jar]
[Loaded sun.awt.image.PixelConverter$Rgbx from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.awt.image.PixelConverter$Bgrx from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.awt.image.PixelConverter$ArgbBm from C:\Program Files\Java\jre7\lib\
rt.jar]
[Loaded java.awt.image.ColorModel from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.image.PackedColorModel from C:\Program Files\Java\jre7\lib\rt.j
ar]
[Loaded java.awt.image.DirectColorModel from C:\Program Files\Java\jre7\lib\rt.j
ar]
[Loaded java.awt.color.ColorSpace from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.color.ICC_Profile from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.cmm.ProfileDeferralInfo from C:\Program Files\Java\jre7\lib\r
t.jar]
[Loaded sun.java2d.cmm.ProfileDeferralMgr from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded java.awt.color.ICC_ProfileRGB from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded sun.java2d.cmm.ProfileActivator from C:\Program Files\Java\jre7\lib\rt.j
ar]
[Loaded java.awt.color.ICC_Profile$1 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.color.ICC_ColorSpace from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded sun.java2d.StateTrackableDelegate from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.java2d.StateTrackableDelegate$2 from C:\Program Files\Java\jre7\lib\
rt.jar]
[Loaded sun.java2d.pipe.PixelDrawPipe from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded sun.java2d.pipe.PixelFillPipe from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded sun.java2d.pipe.ShapeDrawPipe from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded sun.java2d.pipe.TextPipe from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.pipe.DrawImagePipe from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded sun.java2d.pipe.NullPipe from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.image.IndexColorModel from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded sun.java2d.pipe.ParallelogramPipe from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.java2d.pipe.LoopBasedPipe from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded sun.java2d.pipe.LoopPipe from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.pipe.RenderingEngine from C:\Program Files\Java\jre7\lib\rt.j
ar]
[Loaded sun.java2d.pipe.RenderingEngine$1 from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.dc.DuctusRenderingEngine from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.pipe.OutlineTextRenderer from C:\Program Files\Java\jre7\lib\
rt.jar]
[Loaded sun.java2d.pipe.GlyphListPipe from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded sun.java2d.pipe.GlyphListLoopPipe from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.java2d.pipe.SolidTextRenderer from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.java2d.pipe.AATextRenderer from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded sun.java2d.pipe.LCDTextRenderer from C:\Program Files\Java\jre7\lib\rt.j
ar]
[Loaded sun.java2d.pipe.CompositePipe from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded sun.java2d.pipe.AlphaColorPipe from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded sun.java2d.pipe.PixelToShapeConverter from C:\Program Files\Java\jre7\li
b\rt.jar]
[Loaded sun.java2d.SurfaceData$PixelToShapeLoopConverter from C:\Program Files\J
ava\jre7\lib\rt.jar]
[Loaded sun.java2d.pipe.PixelToParallelogramConverter from C:\Program Files\Java
\jre7\lib\rt.jar]
[Loaded sun.java2d.SurfaceData$PixelToPgramLoopConverter from C:\Program Files\J
ava\jre7\lib\rt.jar]
[Loaded sun.java2d.pipe.TextRenderer from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.pipe.SpanClipRenderer from C:\Program Files\Java\jre7\lib\rt.
jar]
[Loaded sun.java2d.pipe.Region from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.pipe.RegionIterator from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded sun.java2d.pipe.Region$ImmutableRegion from C:\Program Files\Java\jre7\l
ib\rt.jar]
[Loaded sun.java2d.pipe.AAShapePipe from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.pipe.AlphaPaintPipe from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded sun.java2d.pipe.SpanShapeRenderer from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.java2d.pipe.SpanShapeRenderer$Composite from C:\Program Files\Java\j
re7\lib\rt.jar]
[Loaded sun.java2d.pipe.GeneralCompositePipe from C:\Program Files\Java\jre7\lib
\rt.jar]
[Loaded sun.java2d.pipe.DrawImage from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.loops.RenderCache from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.loops.RenderCache$Entry from C:\Program Files\Java\jre7\lib\r
t.jar]
[Loaded sun.java2d.DestSurfaceProvider from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded java.awt.Image from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.image.VolatileImage from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.image.SunVolatileImage from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded java.awt.ImageCapabilities from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.image.SurfaceManager$ImageAccessor from C:\Program Files\Java\jr
e7\lib\rt.jar]
[Loaded java.awt.Image$1 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.image.SurfaceManager from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.util.EventListener from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.DisplayChangedListener from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded sun.awt.image.VolatileSurfaceManager from C:\Program Files\Java\jre7\lib
\rt.jar]
[Loaded java.awt.image.ImageObserver from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.MenuContainer from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.Component from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.Component$AWTTreeLock from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded java.util.EventObject from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.AWTEvent from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.event.ComponentEvent from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded java.awt.event.InputEvent from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AWTAccessor$AWTEventAccessor from C:\Program Files\Java\jre7\lib
\rt.jar]
[Loaded java.awt.AWTEvent$1 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AWTAccessor from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.event.NativeLibLoader from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded sun.awt.AWTAccessor$InputEventAccessor from C:\Program Files\Java\jre7\l
ib\rt.jar]
[Loaded java.awt.event.InputEvent$1 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.peer.ComponentPeer from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.dnd.peer.DropTargetPeer from C:\Program Files\Java\jre7\lib\rt.
jar]
[Loaded sun.awt.windows.WObjectPeer from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.windows.WComponentPeer from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded java.awt.Font from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.font.FontAccess from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.Font$FontAccessImpl from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.geom.AffineTransform from C:\Program Files\Java\jre7\lib\rt.jar
]
[Loaded sun.font.AttributeValues from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.font.EAttribute from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.text.AttributedCharacterIterator$Attribute from C:\Program Files\Ja
va\jre7\lib\rt.jar]
[Loaded java.lang.Class$4 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.reflect.NativeMethodAccessorImpl from C:\Program Files\Java\jre7\lib
\rt.jar]
[Loaded sun.reflect.DelegatingMethodAccessorImpl from C:\Program Files\Java\jre7
\lib\rt.jar]
[Loaded java.awt.font.TextAttribute from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AWTAccessor$ComponentAccessor from C:\Program Files\Java\jre7\li
b\rt.jar]
[Loaded java.awt.Component$1 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.RequestFocusController from C:\Program Files\Java\jre7\lib\rt.ja
r]
[Loaded java.awt.Component$DummyRequestFocusController from C:\Program Files\Jav
a\jre7\lib\rt.jar]
[Loaded sun.awt.windows.WToolkit$1 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.beans.PropertyChangeSupport from C:\Program Files\Java\jre7\lib\rt.
jar]
[Loaded java.awt.Toolkit$DesktopPropertyChangeSupport from C:\Program Files\Java
\jre7\lib\rt.jar]
[Loaded java.beans.ChangeListenerMap from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.beans.PropertyChangeSupport$PropertyChangeListenerMap from C:\Progr
am Files\Java\jre7\lib\rt.jar]
[Loaded java.beans.PropertyChangeListener from C:\Program Files\Java\jre7\lib\rt
.jar]
[Loaded sun.awt.ModalityListener from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.SunToolkit$ModalityListenerList from C:\Program Files\Java\jre7\
lib\rt.jar]
[Loaded sun.misc.PerformanceLogger from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.misc.PerformanceLogger$TimeData from C:\Program Files\Java\jre7\lib\
rt.jar]
[Loaded sun.java2d.DisposerRecord from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.windows.WToolkit$ToolkitDisposer from C:\Program Files\Java\jre7
\lib\rt.jar]
[Loaded sun.java2d.Disposer from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.java2d.Disposer$1 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AWTAutoShutdown from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.util.IdentityHashMap from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.windows.WToolkit$2 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.windows.WToolkit$2$1 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.lang.ApplicationShutdownHooks from C:\Program Files\Java\jre7\lib\r
t.jar]
[Loaded java.lang.ApplicationShutdownHooks$1 from C:\Program Files\Java\jre7\lib
\rt.jar]
[Loaded java.lang.Shutdown from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AppContext from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.misc.JavaAWTAccess from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AppContext$6 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AppContext$3 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AppContext$2 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AppContext$State from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AppContext$1 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.EventQueue from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.EventQueue$1 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.AWTAccessor$EventQueueAccessor from C:\Program Files\Java\jre7\l
ib\rt.jar]
[Loaded java.awt.EventQueue$2 from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.Queue from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.MostRecentKeyValue from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.PostEventQueue from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.peer.DesktopPeer from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded sun.awt.windows.WDesktopPeer from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.awt.Desktop$Action from C:\Program Files\Java\jre7\lib\rt.jar]
[Loaded java.util.IdentityHashMap$KeySet from C:\Program Files\Java\jre7\lib\rt.
jar]
[Loaded java.util.IdentityHashMap$IdentityHashMapIterator from C:\Program Files\
Java\jre7\lib\rt.jar]
[Loaded java.util.IdentityHashMap$KeyIterator from C:\Program Files\Java\jre7\li
b\rt.jar]
...编辑 我的更新代码:
protected async void Login_click(object sender, EventArgs e)
{
response1.Text = "Started";
var tentantID = ConfigurationManager.AppSettings["tenantID"];
var clientId = ConfigurationManager.AppSettings["applicationID"];
var secret = ConfigurationManager.AppSettings["secret"];
await Authorize(tentantID , clientId, secret);
}
private async Task<AuthenticationResult> GetToken(string clientId, string tenantDomain, string clientSecret)
{
AuthenticationResult result = null;
var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantDomain);
try
{
ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
return result = await context.AcquireTokenAsync("https://management.core.windows.net/", clientCredential).ConfigureAwait(false);
}
catch (AdalException ae)
{
//Error code
return null;
}
catch (Exception e)
{
//Error code
return null;
}
}
private async Task Authorize(string tenant, string clientId, string clientSecret)
{
var authenticationResult = await GetToken(clientId, tenant, clientSecret).ConfigureAwait(false);
string token = authenticationResult.AccessToken;
}
答案 0 :(得分:0)
async void
方法并不是一件好事。有关它的一些信息,请参阅https://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx。
我相信您的Login_Click
方法应使用此行异步调用Authorize
:
RegisterAsyncTask(new PageAsyncTask(Authorize(tentantID, clientId, secret)));
然后Login_Click
应该返回void
而不使用async
关键字。
答案 1 :(得分:0)
根据您的描述,我通过在.NET Framework 4.5上创建Web表单应用程序目标并引用Active Directory Authentication Library 3.13.8来测试此问题。根据您的代码,它可以按预期工作并部署到Azure Web App。这是我的git sample,您可以参考它并尝试找出它是否可以按预期工作。